<?php
/**
 * WebProduction Shop (wpshop)
 * @copyright (C) 2011-2012 WebProduction (tm) <webproduction.com.ua>
 *
 * This program is commercial software; you cannot redistribute it and/or
 * modify.
 */

/**
 * @author Egor Gerasimchuk <milhous@webproduction.ua>
 * @copyright WebProduction
 * @package Shop
 */
class Photo extends XPhoto{
    public function __construct($id = 0) {
        parent::__construct($id);
        $this->setClassname(__CLASS__);
    }

    /**
     * @return Photo
     */
    public static function Get($key) {
        return self::GetObject("Photo", $key);
    }

    /**
     * @return Photo
     */
    public function getNext() {
        return parent::getNext();
    }

    /**
     * @param int $width
     * @param bool $height
     * @param string $method
     * @return bool|mixed
     */
    public function makeImageThumb($width = 60, $height = 60, $method = 'prop') {
        $src = $this->getImage();
        $dir = MEDIA_PATH . 'photoalbum/';
        if (!$src || !file_exists($dir . $src)) {
            return;
        } else {
            $src = $dir . $src;
            PackageLoader::Get()->import('ImageProcessor');
            if ($method == 'prop') {
                $x = ImageProcessor_Thumber::MakeThumbProportional($src, $width, $height);
            } else {
                $x = ImageProcessor_Thumber::MakeThumbCrop($src, $width, $height);
            }
            $x = str_replace(PROJECT_PATH, '/', $x);
            return $x;
        }
    }

    /**
     * @param int $width
     * @param bool $height
     * @param string $method
     * @return bool|mixed
     */
    public function makeBigImageThumb($width = 800, $height = 800) {
        $src = $this->getImage();
        $dir = MEDIA_PATH . 'photoalbum/';
        if (!$src || !file_exists($dir . $src)) {
            return;
        } else {
            $sizeArray = @getimagesize(MEDIA_PATH.'/photoalbum/'.$this->getImage());
            if ($sizeArray) {
                if  ($sizeArray[0] > $width || $sizeArray[1] > $height) {
                    if ($sizeArray[0] > $width) {
                        $x = $this->makeImageThumb($width, false);
                    } elseif ($sizeArray[1] > $height) {
                        $x = $this->makeImageThumb(false, $height);
                    }

                } else {
                    $x = '/media/photoalbum/'.$this->getImage();
                }
            }
            return $x;
        }
    }
}
